/*
 * @(#)Base64Util.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.string;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Base64Util
 * 
 * @author BIS 2012-12-24
 */
public class Base64Util
{

    /**
     * 主测试方法。
     * @Description:
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException
    {
        String str = "lidq测试123";
        byte[] bytes = str.getBytes();
        System.out.println(Base64Util.encodeString(str));
        System.out.println(Base64Util.decodeString(Base64Util.encodeString(bytes, "UTF-8"), "UTF-8"));
    }

    /***
     * 将Base64字节数组转换成字符串。
     * 
     * @param bytes
     * @return
     */
    public static String encodeString(byte[] bytes)
    {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    /**
     * 将字符串按照编码格式转换相应的字符串。
     * @Description:
     * @param bytes
     * @param charset
     * @return
     */
    public static String encodeString(byte[] bytes, String charset)
    {
        String result = "";
        BASE64Encoder encoder = new BASE64Encoder();
        String str = new String(bytes);
        try
        {
            result = encoder.encode(str.getBytes(charset));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /***
     * 将字符串按照UTF-8格式转换成Base64字符串。
     * 
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeString(String str)
    {
        String result = "";
        if (str != null)
        {
            BASE64Encoder encoder = new BASE64Encoder();
            try
            {
                result = encoder.encode(str.getBytes("UTF-8"));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将字符串按照给定编码转换成Base64字符串。
     * @Description:
     * @param str
     * @param charset
     * @return
     */
    public static String encodeString(String str, String charset)
    {
        String result = "";
        if (str != null)
        {
            BASE64Encoder encoder = new BASE64Encoder();
            try
            {
                result = encoder.encode(str.getBytes(charset));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }

    /***
     * 将Base64字符串解析成字符串。
     * 
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decodeString(String str)
    {
        String result = "";
        if (str != null)
        {
            BASE64Decoder base64 = new BASE64Decoder();
            byte[] deStr = null;
            try
            {
                deStr = base64.decodeBuffer(str);
                result = new String(deStr, "UTF-8");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将Base64字符串按照编码格式解析成字符串。
     * @Description:
     * @param str
     * @param charset
     * @return
     */
    public static String decodeString(String str, String charset)
    {
        String result = "";
        if (str != null)
        {
            BASE64Decoder base64 = new BASE64Decoder();
            byte[] deStr = null;
            try
            {
                deStr = base64.decodeBuffer(str);
                result = new String(deStr, charset);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}